/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：SysPermission仓储                                              
*│　作    者：sys                                            
*│　创建时间：2020-11-06 14:41:01  
*│　说    明：代码生成器生成   
*└──────────────────────────────────────────────────────────────┘
*/
using NPOI.SS.Formula.Functions;
using SXFramWork.Core;
using SXFramWork.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SXFramWork.Repository
{
    public class SysPermissionRepository : RepositoryAbstract<SysPermissionEntity>, ISysPermissionRepository
    {
        public SysPermissionRepository(IDbContext dbContext):base(dbContext)
        {
          
        }

        public async Task<List<PermissionTreeModel>> GetAllPermission()
        {
            return await FromSql.GetDataListAsync<PermissionTreeModel>(@"SELECT [Id],[Platform],[Name] label,'0'checkArr,(case when ParentId is null then '0' when ParentId='' then '0' else ParentId end)parentId FROM [SysPermission]");
        }

        public async Task<List<SysPermissionEntity>> GetMenuAsync(string userId)
        {
            var sql = @"select distinct  sp.Id,sp.Name,sp.Code,sp.Path,sp.Icon,sp.Sort,sp.CreateTime,sp.Remarks,sp.ParentId from SysPermission sp 
            left join SysRolePermission srp on sp.Id=srp.PermissionId 
            left join SysUserRole sur on sur.SysRoleId=srp.RoleId  where sur.SysUserId=@UserId  and sp.Type=0 and sp.Enabled=1 order by sort asc";
            return await FromSql.GetDataListAsync(sql, new { UserId = userId }) as List<SysPermissionEntity>;
        }

        public async Task<List<SysPermissionEntity>> GetAllMenus()
        {
            var sql = @"select distinct  sp.Id,sp.Name,sp.Code,sp.Path,sp.Icon,sp.Sort,sp.CreateTime,sp.Remarks,sp.ParentId from SysPermission sp 
            left join SysRolePermission srp on sp.Id=srp.PermissionId 
            left join SysUserRole sur on sur.SysRoleId=srp.RoleId  where sp.Type=0 and sp.Enabled=1 order by sort asc";
            return await FromSql.GetDataListAsync(sql) as List<SysPermissionEntity>;
        }

        public async Task<List<SysPermissionEntity>> Query(MenuQueryModel model)
        {
            var sql = @"SELECT * FROM [SysPermission] where type=0  ";
            if (model.Name.NotNull())
            {
                sql += " and Name like '%" + model.Name + "%'";
            }
            sql += " order by sort asc";
            var res = await FromSql.GetDataListAsync<SysPermissionEntity>(sql);
            return res;
        }

        public async Task<List<SysPermissionEntity>> QueryChildren(string id)
        {
           var sql = @";with temp as(select * from SysPermission where id=@id
                union all
                select a.* from SysPermission a join temp b on a.ParentId=b.Id where b.Id is not null and b.Id <>''
                )
                select* from temp where [Type]=0";
            return await FromSql.GetDataListAsync<SysPermissionEntity>(sql, new { id });
        }

        public async Task<List<string>> QueryCodeByUserId(string userId)
        {
            return await FromSql.GetDataListAsync<string>(@"select distinct code from SysPermission sp left join SysRolePermission srp on sp.Id = srp.PermissionId left join SysUserRole sur on sur.SysRoleId = srp.RoleId where sur.SysUserId =@uid ", new { uid = userId });
        }

        public async Task<List<string>> GetPermissionByUId(string uid)
        {
            var sql = @"select distinct sp.code from SysPermission sp 
            left join SysRolePermission srp on sp.Id=srp.PermissionId 
            left join SysUserRole sur on sur.SysRoleId=srp.RoleId  where sur.SysUserId=@UserId  and sp.Type=1 and sp.Enabled=1";
            return await FromSql.GetDataListAsync<string>(sql, new { UserId = uid }) as List<string>;
        }

        public async Task<List<string>> GetPermission()
        {
            var sql = @"select distinct sp.code from SysPermission sp 
            left join SysRolePermission srp on sp.Id=srp.PermissionId 
            left join SysUserRole sur on sur.SysRoleId=srp.RoleId  where sp.Type=1 and sp.Enabled=1";
            return await FromSql.GetDataListAsync<string>(sql) as List<string>;
        }
    }
}